package com.lap.framework.base.tools;

import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.converter.ArgumentConversionException;
import org.junit.jupiter.params.converter.ArgumentConverter;

/**
 *
 *
 * <pre>
 * 参数格式:
 * '[dict.test.a,dict.test.b]'
 *
 * 使用方式:
 * {@code
 * @ConvertWith(StringConverter.class) List<String> dataList
 * }
 * </pre>
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
public class StringConverter implements ArgumentConverter {

  @Override
  public Object convert(Object source, ParameterContext context)
      throws ArgumentConversionException {
    if (source instanceof String arg) {
      arg = arg.trim().substring(1, arg.trim().length() - 1).trim();
      if (arg.isEmpty()) return new String[] {};

      return Stream.of(arg.split("[,;]")).toList();
    }
    return source;
  }
}
